// pages/leaveHospital/index.ts
import request from '../../utils/request'
import Dialog from '@vant/weapp/dialog/dialog'
const app = getApp<IAppOption>()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        globalData: {},

        patientList: [],// 患者列表
        allSelected: false,// 全选
        selectData: [],// 选中的数据
        
        popup: {},// 提示框

    },

    /**
     * 全选/取消全选
     * @param e 
     */
    selectAll(e: any) {
        const patientList = this.data.patientList;
        let values:any = [];
        if(! this.data.allSelected){  //全选
            patientList.forEach( obj => {
                obj.checked = true;
                values.push(obj.visitNo);
            })
        } else {  //取消全选，即全不选
            patientList.forEach( obj => {
                obj.checked = false;
            })
        }

        this.setData({
            allSelected: !this.data.allSelected,
            patientList: patientList,
            selectData: values,
        })
    },
    /**
     * 选中/取消选中
     * @param e 
     */
    checkboxChange(e: any) {
        const values = e.detail.value
        // 判断是否全选
        let flag = false;
        if(values && values.length === this.data.patientList.length) {
            flag = true;
        }
        this.setData({
            allSelected: flag,
            selectData: values
        })
    },

    // 加载患者
    getPatient(){
        let that = this;
        request.get('/applet/patient/data', { dataType: app.globalData.dataType }).then((res: any) => {
            if(res.data.code === 200) {

                let data = res.data.data;
                let list:any = [];
                if(data && data.length > 0) {
                    data.forEach(val => {
                        val.checked = false;
                        list.push(val);
                    });
                }

                that.setData({
                    patientList: list
                });
            }
        })
    },

    doSubmit(){
        let that = this;
        let data = that.data.selectData
        if(data.length <= 0) {
            that.setData({
                popup: {
                    idx: Math.random(),
                    show: true,
                    title: '提 示',
                    msg: '请先选择患者再出院！',
                    icon: '../../static/img/no_happy.png'
                },
            })
            return ;
        }

        Dialog.confirm({
            title: '您确定要将所选患者标记为出院吗？',
            confirmButtonColor: "#00A991"
        }).then(() => {
            wx.showLoading({
                title: '请求中…',
            })
            request.post('/applet/patient/leaveHospital', data).then((res: any) => {
                wx.hideLoading()
                that.setData({
                    popup: {
                        idx: Math.random(),
                        show: true,
                        title: '提 示',
                        msg: '患者出院成功！',
                        icon: '../../static/img/smile.png'
                    },
                });
    
                that.getPatient();
            })
        }).catch(() => {
            // on cancel
        });
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            globalData: app.globalData
        });

        this.getPatient();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})